package librarymanager

import grails.test.mixin.TestFor
import spock.lang.Specification

/**
 * See the API for {@link grails.test.mixin.domain.DomainClassUnitTestMixin} for usage instructions
 */
@TestFor(Book)
class BookSpec extends Specification {

    def setup() {
    }

    def cleanup() {
    }

    void "Test the title's size constraint"() {
        when: "The title's size is less than 3 characters"
            def a = new Book(title: "Te", category: Book.Category.PUBLIC)
        
        then: "The test should fail"
            !a.validate()
        
        when: "The title's size is over than 50 characters"
            a = new Book(title: "This is a test title which size is over than 50 characters", category: Book.Category.PUBLIC)
        
        then: "The test should fail"
            !a.validate()
        
        when: "The title's size is more than 3 and less than 50"
            a = new Book(title: "Book Test 2", category: Book.Category.PUBLIC)
        
        then: "The test should pass"
            a.validate()
    }
    
    void "Test the title nullable constraint"() {
        when: "The title is null"
            def a = new Book(category: Book.Category.PUBLIC)
            
        then: "The test should fail"
            !a.validate()
    }
    
    void "Test the title blank constraint"() {
        when: "The title is blank"
            def a = new Book(title: "", category: Book.Category.PUBLIC)
            
        then: "The test should fail"
            !a.validate()
    }
    
    void "Test the category nullable constraint"() {
        when: "The category is null"
            def a = new Book(title: "Valid Title")
            
        then: "The test should fail"
            !a.validate()
    }
    
    void "Test the category blank constraint"() {
        when: "The category is blank"
            def a = new Book(title: "Valid Title",category: "")
            
        then: "The test should fail"
            !a.validate()
    }
}
